<template>
  <div class="app-container">
    <el-form v-show="showSearch" ref="queryForm" :model="queryParams" size="small" :inline="true" label-width="68px">
      <el-form-item label="事件名称" prop="accName">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入事件名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="公司名称" prop="compName">
        <el-input
          v-model="queryParams.corpName"
          placeholder="请输入公司名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="问题级别" prop="level">
        <el-select v-model="queryParams.level" placeholder="请选择当前状态" clearable>
          <el-option
            v-for="dict in dict.type.sys_dict_problem_level"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="当前状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择当前状态" clearable>
          <el-option
            v-for="dict in dict.type.sys_task_handle_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          v-hasPermi="['accident:basic:add']"
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          v-hasPermi="['accident:basic:update']"
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          v-hasPermi="['accident:basic:delete']"
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          v-hasPermi="['accident:basic:export']"
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
        >导出</el-button>
      </el-col>
      <right-toolbar :show-search.sync="showSearch" @queryTable="getList" />
    </el-row>

    <el-table v-loading="loading" :data="infoList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="事件名称" align="center" prop="accName" />
      <el-table-column label="公司名称" align="center" prop="corpName" />
      <el-table-column label="问题级别" align="center" prop="accLevel">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_dict_problem_level" :value="scope.row.accLevel" />
        </template>
      </el-table-column>
      <el-table-column label="当前状态" align="center" prop="status">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_task_handle_type" :value="scope.row.status" />
        </template>
      </el-table-column>
      <el-table-column label="修改人员" align="center" prop="modifyBy" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            v-hasPermi="['accident:basic:update']"
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
          >修改</el-button>
          <el-button
            v-hasPermi="['accident:basic:delete']"
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
          >删除</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-tickets"
            @click="goDetailPage(scope.row)"
          >详情</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNo"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改问题基础对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="事件名称" prop="accName">
          <el-input v-model="form.accName" placeholder="请输入事件名称" />
        </el-form-item>
        <el-form-item label="公司名称" prop="compName">
          <el-select
            v-model="form.corp"
            filterable
            remote
            placeholder="请输入关键词查询"
            :remote-method="searchCorp"
            :loading="searchLoading"
          >
            <el-option
              v-for="item in corpList"
              :key="item.id"
              :label="item.name"
              :value="item.corp"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="问题级别" prop="accLevel">
          <el-select v-model="form.accLevel">
            <el-option
              v-for="dict in dict.type.sys_dict_problem_level"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="当前状态" prop="status">
          <el-select v-model="form.status">
            <el-option
              v-for="dict in dict.type.sys_task_handle_type"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            />
          </el-select>
        </el-form-item>
        <!-- <el-form-item label="修改人员" prop="modifyBy">
          <el-input v-model="form.modifyBy" placeholder="请输入修改人员" />
        </el-form-item>
        <el-form-item label="发生问题任务id" prop="taskId">
          <el-input v-model="form.taskId" placeholder="请输入发生问题任务id" />
        </el-form-item>-->
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listInfo, getInfo, delInfo, addInfo, updateInfo } from '@/api/accident/api'
import { listClient } from '@/api/client/client'

export default {
  name: 'Info',
  dicts: ['sys_task_handle_type', 'sys_dict_problem_level'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 问题基础表格数据
      infoList: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNo: 1,
        pageSize: 10,
        accName: null,
        corpName: null,
        level: null,
        status: null
      },
      // 表单参数
      form: {},

      corpList: [],
      searchLoading: false,
      // 表单校验
      rules: {
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    /** 查询问题基础列表 */
    getList() {
      this.loading = true
      listInfo(this.queryParams).then(response => {
        this.infoList = response.data.records
        this.total = response.data.total
        this.loading = false
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        accName: null,
        compName: null,
        compId: null,
        accLevel: null,
        status: null,
        createTime: null,
        updateTime: null,
        createBy: null,
        modifyBy: null,
        taskId: null
      }
      this.resetForm('form')
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNo = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm')
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.open = true
      this.title = '添加问题基础'
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      const id = row.id || this.ids

      getInfo(id).then(response => {
        this.form = response.data
        this.form.corp = this.form.corpId + '/' + this.form.corpName
        this.open = true
        this.title = '修改问题基础'
      })
    },
    searchCorp(query) {
      const params = {
        pageNo: 1,
        pageSize: 10,
        customName: query
      }
      this.searchLoading = true
      listClient(params).then(response => {
        const data = response.data.records.map(item => ({
          ...item,
          corp: `${item.id}/${item.name}`
        }))
        this.corpList = data
        this.searchLoading = false
      })
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs['form'].validate(valid => {
        if (valid) {
          const formData = {
            id: '',
            ...this.form,
            corpId: this.form.corp.split('/')[0],
            corpName: this.form.corp.split('/')[1]
          }
          delete formData.corp
          if (this.form.id != null) {
            updateInfo(formData).then(response => {
              this.$modal.msgSuccess('修改成功')
              this.open = false
              this.getList()
            })
          } else {
            addInfo(formData).then(response => {
              this.$modal.msgSuccess('新增成功')
              this.open = false
              this.getList()
            })
          }
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids
      this.$modal.confirm('是否确认删除问题基础编号为"' + ids + '"的数据项？').then(function() {
        return delInfo(ids)
      }).then(() => {
        this.getList()
        this.$modal.msgSuccess('删除成功')
      }).catch(() => {})
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/info/export', {
        ...this.queryParams
      }, `info_${new Date().getTime()}.xlsx`)
    },
    goDetailPage(row) {
      this.$router.push({
        name: 'ClientAccidentDetail',
        params: { certNo: this.corpId || '1', id: row.id || '1' }
      })
    }
  }
}
</script>
